package com.example.guiguihomemedical.controller;

import com.example.guiguihomemedical.common.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;

@RestController
@RequestMapping("/test")
@CrossOrigin
public class UpLoadPictureController {
    @PostMapping("/upload")
    public Result upload(MultipartFile uploadfile) throws IOException {

        //1、输出测试
        System.out.println("==============");
        System.out.println(uploadfile);
        System.out.println(uploadfile.getName());
        System.out.println(uploadfile.getSize());
        //2、将上传的图片存储到硬盘
        InputStream inputStream = uploadfile.getInputStream();
        FileChannel inChannel = (FileChannel) Channels.newChannel(inputStream);
        FileChannel outChannel = new FileOutputStream("./a.jpg").getChannel();//当前目录下，命名为a.jpg
        inChannel.transferTo(0,inChannel.size(),outChannel);

        //3、关闭流
        inChannel.close();
        outChannel.close();
        inputStream.close();

        //4、返回成功信息
        return Result.success();
    }

}
